package com.xinggq.pay.service.pay.channel.yse.pay.desk;

import cn.hutool.core.lang.Validator;
import com.xinggq.pay.api.dto.YseCommonPayReqDTO;
import com.xinggq.pay.api.exception.PayException;
import com.xinggq.pay.service.adaptor.YseApiAdaptor;
import com.xinggq.pay.service.pay.channel.yse.enums.YseMethodEnum;
import com.xinggq.pay.service.pay.channel.PayChannelPayApiService;
import com.xinggq.pay.service.entity.YsePayConfigEntity;
import com.xinggq.pay.service.pay.channel.yse.enums.YsePayWayBindEnum;
import com.xinggq.pay.service.pay.channel.yse.pay.desk.dto.YsePayDeskRespDTO;
import com.xinggq.pay.service.service.YsePayConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 银盛B2B& B2C收银台支付实现类
 * @Description
 * @Author xinggq
 * @Date 2025/9/12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class YseDeskPayServiceImpl implements
    PayChannelPayApiService<YseCommonPayReqDTO,YsePayDeskRespDTO> {

  private final YseApiAdaptor yseApiAdaptor;
  private final YsePayConfigService ysePayConfigService;

  @Override
  public YsePayDeskRespDTO pay(YseCommonPayReqDTO payParamDto) {
    if (isDeskPayWay(payParamDto)) {
      // 获取数据库中的银盛当前支付渠道配置
      YsePayConfigEntity ysePayConfig = ysePayConfigService.getConfigByMethod(YseMethodEnum.DESK_PAY);
      Validator.validateNotNull(ysePayConfig,
          String.format("银盛接口配置为空，支付方式:{%s}", YseMethodEnum.DESK_PAY.getMethod()));
      // 当前支付方式由后端组装参数给前端，由前端拉起支付页面
      return YsePayDeskRespDTO.buildYsePayDeskDTO(payParamDto, ysePayConfig);
    } else {
      throw new PayException("不支持的银盛接口调用");
    }
  }


  private boolean isDeskPayWay(YseCommonPayReqDTO payParamDto) {
    if (payParamDto.getYsePayWay() == null) {
      // 默认B2B&B2C收银台支付
      payParamDto.setYsePayWay(YsePayWayBindEnum.YSE_PAY_DESK.getPayWayEnum());
      return true;
    }
    return payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_PAY_DESK.getPayWayEnum();
  }


}
